

let songslist = [] //播放列表
let musicId =  -1
let playlistId = -1
let nowPlayingIndex = -1
const db = wx.cloud.database()
let music ={}

//背景音乐管理器
// const  backgroundAudioManager = wx.getbackgroundAudioManager()
let InnerAudioContext = wx.createInnerAudioContext();
const app = getApp();
Page({
 
  data: {
    songInfo:{},
    picUrl:"",
    isPlaying: true, //false表示不播放，true表示播放
    lyric:"",
    recordIsShow:true,
    isSame:false,  //判断是否为同一首歌，false表示不是同一首歌
    // InnerAudioContext:null
  },
  //切换唱片的显示
  onToggleChangeRecord(){
    this.setData({
      recordIsShow:!this.data.recordIsShow
    })
  },

  //把currentTime传给子组建lyric
  timeUpdate(event){
    this.selectComponent('.lyric').undate(event.currentTime);
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      nowPlayingIndex = options.index-0;
      musicId = options.musicId-0;
      songslist = wx.getStorageSync('songlist');
      let music = songslist[nowPlayingIndex];
      this.setData({
        songInfo: music,
        isPlaying:app.getMusicPlay()
      })
     
      this._loadMusicDetail(musicId);
     
  },
  _loadMusicDetail(musicId){
    if(musicId == app.getplayMusicId()){
     this.setData({
       isSame:true
      })
    }else{
      this.setData({
        isSame:false
       })
    }
    app.setplayMusicId(musicId);
    let music = songslist[nowPlayingIndex];
    this.setData({
      songInfo: music,
      isPlaying:app.getMusicPlay()
    })
    if(!this.data.isSame){   
      this.setBackgroundMusic(music);
      //保存播放历史
      this.savhistory();
    }
    
    //监听背景音频
    this._bindBGMEvent();
  },
  // 设置音乐标题，lianjie
  setBackgroundMusic(music){
   
    if(InnerAudioContext != null){
      InnerAudioContext.stop();
    }
    InnerAudioContext.destroy();
   InnerAudioContext = wx.createInnerAudioContext();
    this.setData({
      InnerAudioContext
    })
    InnerAudioContext.src = music.songUrl;
   InnerAudioContext.loop = true;
    
    // this.setData({
    //   isPlaying:true
    // })
    
  },
//保存历史记录
savhistory(){
  let music =songslist[nowPlayingIndex]
  let openid = app.globalData.openid;
  let history = wx.getStorageSync(openid)||[]
  let bhave = false
  for(let i = 0; i< history.length;i++){
    if(history[i].musicId == music.musicId){
      bhave = true
    }
  }
  if(!bhave){
    history.unshift(music)
    wx.setStorage({
      key:openid,
      data:history
    })
  }
},

  //播放暂停切换
  togglePlaying(){
    if(this.data.isPlaying){ 
      InnerAudioContext.pause()
      app.setMusicPlay(false)
    }else{
      InnerAudioContext.play()
      app.setMusicPlay(true)
    }
    this.setData({
      isPlaying:!app.getMusicPlay()
    })
  },
  //上一首
  onPre(){
      nowPlayingIndex--
      if(nowPlayingIndex < 0){
        nowPlayingIndex = songslist.length-1;
      }
      app.setMusicPlay(true)
      this._loadMusicDetail(songslist[nowPlayingIndex].musicId);
  },
  //下一首
  onNext(){
      nowPlayingIndex++
      if(nowPlayingIndex > songslist.length-1){
        nowPlayingIndex = 0
       }
       app.setMusicPlay(true)
       this._loadMusicDetail(songslist[nowPlayingIndex].musicId);
  },
  _bindBGMEvent(){
    InnerAudioContext.onCanplay(()=>{
      InnerAudioContext.play();
    })
     InnerAudioContext.onPlay(()=>{
      
      this.setData({
        isPlaying:app.getMusicPlay()
      })
    })
    InnerAudioContext.onStop(()=>{
        
    })
     InnerAudioContext.onPause(()=>{
    
      this.setData({
        isPlaying:false
      })
    })
    InnerAudioContext.onEnded(()=>{
     this.onNext();
    })
     InnerAudioContext.onTimeUpdate(()=>{
        let currentTime = InnerAudioContext.currentTime;
        //联动歌词
        this.timeUpdate({
          currentTime
        })
    })
    InnerAudioContext.onError((error)=>{
      console.log(error);
    }) 

  },
})